#!/bin/bash

echo "=== RocketMQ 快速修复脚本 ==="
echo "正在修复连接问题..."

# 1. 停止现有服务
echo "1. 停止现有服务..."
docker-compose down

# 2. 清理网络
echo "2. 清理Docker网络..."
docker network prune -f

# 3. 清理日志
echo "3. 清理日志目录..."
rm -rf ./logs/*

# 4. 验证配置文件
echo "4. 验证配置文件..."
if grep -q "192.168.211.139" broker.conf; then
    echo "✓ broker.conf 配置正确"
else
    echo "✗ broker.conf 配置错误，请检查IP地址"
    exit 1
fi

# 5. 重新启动服务
echo "5. 重新启动服务..."
docker-compose up -d

# 6. 等待服务启动
echo "6. 等待服务启动..."
sleep 15

# 7. 检查服务状态
echo "7. 检查服务状态..."
docker-compose ps

# 8. 检查关键日志
echo "8. 检查关键日志..."
echo "--- NameServer 日志 ---"
docker logs rmqnamesrv --tail 10 | grep -E "(start|error|exception)"

echo "--- Broker 日志 ---"
docker logs rmqbroker --tail 10 | grep -E "(start|error|exception)"

# 9. 测试连接
echo "9. 测试网络连接..."
echo "测试端口 9876 (NameServer)..."
if nc -z 192.168.211.139 9876 2>/dev/null; then
    echo "✓ NameServer 端口可访问"
else
    echo "✗ NameServer 端口不可访问"
fi

echo "测试端口 10911 (Broker)..."
if nc -z 192.168.211.139 10911 2>/dev/null; then
    echo "✓ Broker 端口可访问"
else
    echo "✗ Broker 端口不可访问"
fi

echo ""
echo "=== 修复完成 ==="
echo "如果仍有问题，请检查："
echo "1. 防火墙设置"
echo "2. 网络配置"
echo "3. 查看详细日志：docker logs rmqbroker"
echo "4. 查看详细日志：docker logs rmqnamesrv"

